<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class ArticleProxy extends Model{
//    protected $table='think_article_proxy';
    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_member.open_id,
        think_member.nickname,think_mch.name as mc_name";
        $list = $this
            ->field($field)
            ->join('think_member',"think_member.id = {$this->getTable()}.mid")
            ->order($this->getTable().'create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * @param $mc_id
     * @param $field
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMchOpenId($mc_id,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_member.open_id,think_member.nickname";
        $list = $this
            ->field($field)
            ->join('think_member',"think_member.id = {$this->getTable()}.uid")
            ->where("think_mch.id",'eq',$mc_id)
            ->find();
        return $list['open_id'];
    }

    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    public function getUpdateTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }

    /**
     * @param int $id
     * @param int $status
     * @param string $desc
     * @param string $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function updateStatus($id=0,$status=0,$desc='',&$out=''){
        $info = $this->find($id);
        if(!$info){
            $out = '申请不存在';
            return false;
        }
        $this->startTrans();
        if($status==3){
            if(!$desc){
                $out = '请输入拒绝理由';
                return false;
            }
            $info->info = $desc;
            $info->status = $status;
            $info->update_time = time();
            if(!$info->save()){
                $this->rollback();
                $out = '操作失败';
                return false;
            }
            $this->commit();
            return true;
        }elseif($status==2){
            $money = $this->getMoney($info['mc_id']);
            $info->status = $status;
            $info->update_time = time();
            $data = [
                'status'=>3,
                'update_time'=>time()
            ];
            $result = $this
                ->where('id','neq',$id)
                ->where('mc_id','eq',$info['mc_id'])
                ->update($data);
            $result1 = $info->save();
            if(!$result && !$result1){
                $this->rollback();
                $out = '操作失败';
                return false;
            }
            $this->commit();
            return $money;
        }else{
            $out = '错误的请求';
            $this->rollback();
            return false;
        }
    }

    /**
     * @param string $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPoxyMidByAid($id=''){
        $info = $this
            ->where('aid','eq',$id)
            ->where('status','eq',2)
            ->find();
        return $info['mid'] ? $info['mid'] : 0;
    }

    /**
     * @param int $id
     * @return float
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMoney($id=0){
        $ids = (new Article())->getIdsByMcid($id);
        $_money = $lottery = (new Lottery())->getMoneyByAids($ids);
        $config = (new Setting())->getDetail();
        $money = (($config['vip'] + $config['used']) * 0.3)
            + ($_money * 0.1 * 0.3);
        return round($money,2);
    }
}